/**=========================================================
 * Directive: datepicker-combine.js
 =========================================================*/

 tzadmin.directive('combineDatepicker', ['Utils',
    function(utils) {
        'use strict';
        
        return {
            restrict: 'EA',
            replace: true,
            template: function($element,$attrs){
                var optionModel = utils.isEmpty($attrs.optionModel) ? 'dateRange' : $attrs.optionModel,
                    ngModel = 'combineDatePickers.' + optionModel,
                    searchFn = utils.isEmpty($attrs.search) ? "search()" : $attrs.search;
                return '\
                    <table>\
                        <tr>\
                            <td style="padding:8px"><datepicker ng-model="' + ngModel + '.startDate"></datepicker></td>\
                            <td style="padding:8px"><span class="control-label">~</span></td>\
                            <td style="padding:8px"><datepicker ng-model="' + ngModel + '.endDate"></datepicker></td>\
                            <td style="padding:8px">\
                                <ui-select ng-model="' + ngModel + '.quickSelects.currentSelect" style="width: 130px;">\
                                    <ui-select-match>\
                                        {{$translate.instant($select.selected.name)}}\
                                    </ui-select-match>\
                                    <ui-select-choices class="animated bounceIn" repeat="option in ' + ngModel + '.quickSelects.options">\
                                        <span>{{$translate.instant(option.name)}}</span>\
                                    </ui-select-choices>\
                                </ui-select>\
                            </td>\
                            <td style="padding:8px">\
                                <a class="btn btn-primary btn-search" ng-click="' + searchFn + '">\
                                    <i class="fa fa-search"></i>\
                                    {{$translate.instant("button.SEARCH")}}\
                                </a>\
                            </td>\
                        </tr>\
                    </table>\
                '
            },
            link: function($scope, $element, $attrs){
                var optionModel = utils.isEmpty($attrs.optionModel) ? 'dateRange' : $attrs.optionModel,
                    ngModel = 'combineDatePickers.' + optionModel,
                    searchFn = utils.isEmpty($attrs.search) ? "search" : $attrs.search.substring(0,$attrs.search.indexOf('('));
         
                $scope.$watch(ngModel + ".quickSelects.currentSelect.value", 
                    function (newVal, oldVal) { 
                        if (newVal != oldVal){
                            var startDate,
                                today = new Date(),
                                endDate = utils.addDate(today, "day", -1);
                            if (newVal == 'past7days'){
                                startDate = utils.addDate(today, "day", -7);
                            }else if (newVal == 'past15days'){
                                startDate = utils.addDate(today, "day", -15);
                            }else if (newVal == 'past30days'){
                                startDate = utils.addDate(today, "day", -30);
                            }else if (newVal == 'past1year'){
                                startDate = utils.addDate(endDate, "year", -1);
                            }
                            
                            $scope.combineDatePickers[optionModel].startDate.date = startDate;
                            $scope.combineDatePickers[optionModel].endDate.date = endDate;
                            
                            // if ($scope[searchFn] instanceof Function){
                            //     $scope[searchFn].call(this);
                            // }
                        }
                    }
                );
                
                $scope.$watch(ngModel + ".startDate.date", 
                    function (newVal, oldVal) {                       
                        $scope.combineDatePickers[optionModel].endDate.dateOptions.minDate = newVal;
                    }
                );
                
                $scope.$watch(ngModel + ".endDate.date", 
                    function (newVal, oldVal) {                       
                        $scope.combineDatePickers[optionModel].startDate.dateOptions.maxDate = newVal;
                    }
                );
            }
        }
    }
]);
